<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <meta name="description" content="钉钉自定义机器人接入首先明确一点，钉钉自定义机器人早就不支持在手机端创建了，所以打开你的pc端或者mac端的钉钉客户端，在需要机器人的聊天群界面，点击智能群助手
​    随后点击添加机器人按钮
​    此时能看到很多已经封装好的第三方机器人，本次我们选择自定义机器人
​    值得一提的是，钉钉" />
  

  
  
  
  
  
  
  <title>钉钉自定义机器人接入 | 南辞的技术博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="钉钉自定义机器人接入首先明确一点，钉钉自定义机器人早就不支持在手机端创建了，所以打开你的pc端或者mac端的钉钉客户端，在需要机器人的聊天群界面，点击智能群助手 ​    随后点击添加机器人按钮 ​    此时能看到很多已经封装好的第三方机器人，本次我们选择自定义机器人 ​    值得一提的是，钉钉的机器人基于webhook协议，webhook呢是一个api概念,是微服务api的使用范式之一,也被">
<meta property="og:type" content="article">
<meta property="og:title" content="钉钉自定义机器人接入">
<meta property="og:url" content="https://yanlidoushikeke.gitee.io/myhexo/2021/03/02/%E9%92%89%E9%92%89%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9C%BA%E5%99%A8%E4%BA%BA%E6%8E%A5%E5%85%A5/index.html">
<meta property="og:site_name" content="南辞的技术博客">
<meta property="og:description" content="钉钉自定义机器人接入首先明确一点，钉钉自定义机器人早就不支持在手机端创建了，所以打开你的pc端或者mac端的钉钉客户端，在需要机器人的聊天群界面，点击智能群助手 ​    随后点击添加机器人按钮 ​    此时能看到很多已经封装好的第三方机器人，本次我们选择自定义机器人 ​    值得一提的是，钉钉的机器人基于webhook协议，webhook呢是一个api概念,是微服务api的使用范式之一,也被">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://v3u.cn/v3u/Public/js/editor/attached/image/20200229/20200229123553_39070.png">
<meta property="og:image" content="https://v3u.cn/v3u/Public/js/editor/attached/image/20200229/20200229123910_82164.png">
<meta property="article:published_time" content="2021-03-02T07:58:06.000Z">
<meta property="article:modified_time" content="2021-03-02T08:15:22.932Z">
<meta property="article:author" content="南辞">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://v3u.cn/v3u/Public/js/editor/attached/image/20200229/20200229123553_39070.png">
  
  
    <link rel="icon" href="/tefuir/css/images/favicon.ico">
  
  
<link rel="stylesheet" href="/tefuir/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/tefuir/atom.xml" title="南辞的技术博客" type="application/atom+xml">
</head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/tefuir/" title="南辞的技术博客" rel="home">南辞的技术博客</a>
      </h1>
      
        <h2 class="site-description hitokoto"></h2>
        <script type="text/javascript" src="https://v1.hitokoto.cn/?encode=js"></script>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/">Home</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/archives">Archives</a></li>
                
                </ul>
            </div>
    </nav>
</header>

      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-钉钉自定义机器人接入" class="post-钉钉自定义机器人接入 post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      钉钉自定义机器人接入
    </h1>
  

        
        <div class="comments-link">
            
            <a href="javascript:void(0);" data-url="https://yanlidoushikeke.gitee.io/myhexo/2021/03/02/%E9%92%89%E9%92%89%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9C%BA%E5%99%A8%E4%BA%BA%E6%8E%A5%E5%85%A5/" data-id="cklrqny3i0000kku020wq70r1" class="leave-reply bdsharebuttonbox" data-cmd="more">Share</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <h3 id="钉钉自定义机器人接入"><a href="#钉钉自定义机器人接入" class="headerlink" title="钉钉自定义机器人接入"></a>钉钉自定义机器人接入</h3><p>首先明确一点，钉钉自定义机器人早就不支持在手机端创建了，所以打开你的pc端或者mac端的钉钉客户端，在需要机器人的聊天群界面，点击智能群助手</p>
<p>​    随后点击添加机器人按钮</p>
<p>​    此时能看到很多已经封装好的第三方机器人，本次我们选择自定义机器人</p>
<p>​    值得一提的是，钉钉的机器人基于webhook协议，webhook呢是一个api概念,是微服务api的使用范式之一,也被成为反向api,即前端不主动发送请求,完全由后端推送，有机会会单门写一篇文章阐述webhook</p>
<p>​    在添加机器人界面里，填写一些机器人的信息</p>
<p>​    <img src="https://v3u.cn/v3u/Public/js/editor/attached/image/20200229/20200229123553_39070.png" alt="img"></p>
<p>​    需要注意的是，在安全设置一栏里，我们选择加签的方式来验证，在此说明一下，钉钉机器人的安全策略有三种，第一种是使用关键字，就是说你推送的消息里必须包含你创建机器人时定义的关键字，如果不包含就推送不了消息，第二种就是使用加密签名，第三种是定义几个ip源，非这些源的请求会被拒绝，综合来看还是第二种又安全又灵活。</p>
<p>​    <img src="https://v3u.cn/v3u/Public/js/editor/attached/image/20200229/20200229123910_82164.png" alt="img"></p>
<p>​    </p>
<p>​    创建成功后，系统会分配给你一个webhook地址，这个地址需要保存一下，地址中有唯一的accesstoken</p>
<p>​    ok，那么怎么利用这个地址让你的机器人推送消息呢？查看官方文档：<a target="_blank" rel="noopener" href="https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq">https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq</a>  </p>
<p>​    发现文档居然还是python2.0的版本，好吧，我们自己来翻译成3.0</p>
<p>​    实现代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line">import hmac</span><br><span class="line">import hashlib</span><br><span class="line">import base64</span><br><span class="line">import urllib.parse</span><br><span class="line"></span><br><span class="line">timestamp &#x3D; str(round(time.time() * 1000))</span><br><span class="line">secret &#x3D; &#39;SEC90485937c351bfaed41fea8eda5f1e155bbf22842d5f9d6871999e05822fd894&#39;</span><br><span class="line">secret_enc &#x3D; secret.encode(&#39;utf-8&#39;)</span><br><span class="line">string_to_sign &#x3D; &#39;&#123;&#125;\n&#123;&#125;&#39;.format(timestamp, secret)</span><br><span class="line">string_to_sign_enc &#x3D; string_to_sign.encode(&#39;utf-8&#39;)</span><br><span class="line">hmac_code &#x3D; hmac.new(secret_enc, string_to_sign_enc, digestmod&#x3D;hashlib.sha256).digest()</span><br><span class="line">sign &#x3D; urllib.parse.quote(base64.b64encode(hmac_code))</span><br><span class="line"># print(timestamp)</span><br><span class="line"># print(sign)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">import requests,json   #导入依赖库</span><br><span class="line">headers&#x3D;&#123;&#39;Content-Type&#39;: &#39;application&#x2F;json&#39;&#125;   #定义数据类型</span><br><span class="line">webhook &#x3D; &#39;https:&#x2F;&#x2F;oapi.dingtalk.com&#x2F;robot&#x2F;send?access_token&#x3D;f0ca7636f5812fe4815c97a72de9a7cc780c414c258b6c9a631036b1d0f49e3b&amp;timestamp&#x3D;&#39;+timestamp+&quot;&amp;sign&#x3D;&quot;+sign</span><br><span class="line">#定义要发送的数据</span><br><span class="line">#&quot;at&quot;: &#123;&quot;atMobiles&quot;: &quot;[&#39;&quot;+ mobile + &quot;&#39;]&quot;</span><br><span class="line">data &#x3D; &#123;</span><br><span class="line">    &quot;msgtype&quot;: &quot;text&quot;,</span><br><span class="line">    &quot;text&quot;: &#123;&quot;content&quot;: &#39;都谁没加到群里来？小心升不了班&#39;&#125;,</span><br><span class="line">    &quot;isAtAll&quot;: True&#125;</span><br><span class="line">res &#x3D; requests.post(webhook, data&#x3D;json.dumps(data), headers&#x3D;headers)   #发送post请求</span><br><span class="line"></span><br><span class="line">print(res.text)</span><br></pre></td></tr></table></figure>

<p>​    </p>

      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/tefuir/2021/03/02/%E9%92%89%E9%92%89%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9C%BA%E5%99%A8%E4%BA%BA%E6%8E%A5%E5%85%A5/">
    <time datetime="2021-03-02T07:58:06.000Z" class="entry-date">
        2021-03-02
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/tefuir/2021/03/03/docker%E5%91%BD%E4%BB%A4/" rel="prev"><span class="meta-nav">←</span> docker命令</a></span>
    
    
        <span class="nav-next"><a href="/tefuir/2021/03/01/Ant/" rel="next">Ant <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->







</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside id="search" class="widget widget_search"><form role="search" method="get" accept-charset="utf-8" id="searchform" class="searchform" action="//google.com/search">
    <div>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="搜索" />
    </div>
</form></aside>
  
    
  
    
  
    
  <aside class="widget">
    <h3 class="widget-title">Recents</h3>
    <div class="widget-content">
      <ul>
        
          <li>
            <a href="/tefuir/2021/04/26/python%E5%9F%BA%E7%A1%80%E7%90%86%E8%AE%BA%E5%AE%9D%E5%85%B8/">python基础理论宝典</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/python3-Flask%E7%BB%93%E5%90%88Socket-io%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E5%9C%A8%E7%BA%BF%E5%AE%A2%E6%9C%8D%E7%B3%BB%E7%BB%9F/">python3+Flask结合Socket.io简单实现在线客服系统</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%88%A9%E7%94%A8docker%E9%83%A8%E7%BD%B2gunicorn-Flask%E6%89%93%E9%80%A0%E7%8B%AC%E7%AB%8B%E9%95%9C%E5%83%8F/">win10系统下利用docker部署gunicorn+Flask打造独立镜像</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/">win10系统下基于docker配置elasticsearch配合python3进行全文检索</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%97%AE%E9%A2%98/">服务器问题</a>
          </li>
        
      </ul>
    </div>
  </aside>

  
    
  
    
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2021 南辞
    All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/js/share.js'];</script>

<script src="/js/jquery-3.3.1.min.js"></script>


  
<link rel="stylesheet" href="/tefuir/fancybox/jquery.fancybox.css">

  
<script src="/tefuir/fancybox/jquery.fancybox.pack.js"></script>




<script src="/tefuir/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>